package com.xyq.fs.scan;

import java.io.File;
import java.io.IOException;

import java.nio.file.Files;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.xyq.fs.base.MyDirectoryS;

import com.xyq.fs.search.MySearch;
import com.xyq.fs.views.WindowsB;
import com.xyq.fs.views.WindwosA;

import javafx.application.Platform;

public class ScanFileInf {

	public static AtomicInteger SCAN_NUM = new AtomicInteger();


	public void scan(String fileType) {

		WindowsB.cacheMyFile.clear();
		long startTime = new Date().getTime();
		SCAN_NUM.set(1);
		ExecutorService pool = Executors.newFixedThreadPool(1);
		ScanFileImpl sf = new ScanFileImpl(pool, fileType);
		for (File f : File.listRoots()) {
			try {
				Files.walkFileTree(f.toPath(), sf);
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.gc();
		}
		sf = null;
		try {
			pool.shutdown();
			pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		SCAN_NUM.set(0);
		WindwosA.getSLEEP_NOW().set(0);
		Platform.runLater(() -> {
			WindwosA.getScanBtn().setText("扫描文件");
			WindwosA.getPin().setVisible(false);
		});
		long endTime = new Date().getTime();
		long cha = endTime - startTime;

		Platform.runLater(() -> {

			if (WindwosA.tsB.getText().length() > 0) {
				if (WindwosA.getCb1().isSelected()) {
					WindwosA.tsB.setText(WindwosA.tsB.getText() + ",扫描完毕,耗时" + formatTime(cha));
					WindwosA.getCb1().setSelected(false);
				} else
					WindwosA.tsB.setText(
							WindwosA.tsB.getText().replaceAll("已扫描的文件数量", "更新的文件数量") + ",扫描完毕,耗时" + formatTime(cha));
			} else {
				WindwosA.tsB.setText("扫描完毕,耗时" + formatTime(cha));
			}
			WindowsB.cacheMyFile.clear();
			if (WindwosA.getCb1().isSelected()) {

				WindwosA.getCb1().setSelected(false);
				// 此操作用后台线程处理,可减少最后显示时的卡顿
				new Thread(() -> {
					MySearch.delOld();
					MyDirectoryS.updateReaderAndSearcher();
					MyDirectoryS.commit();
					System.gc();
				}).start();
			}
			new Thread(() -> {
				MyDirectoryS.updateReaderAndSearcher();
				MyDirectoryS.commit();
				System.gc();
			}).start();
		});
	}

	/*
	 * 毫秒转化时分秒毫秒
	 */
	public static String formatTime(Long ms) {
		Integer ss = 1000;
		Integer mi = ss * 60;
		Integer hh = mi * 60;
		Integer dd = hh * 24;

		Long day = ms / dd;
		Long hour = (ms - day * dd) / hh;
		Long minute = (ms - day * dd - hour * hh) / mi;
		Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
		Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

		StringBuffer sb = new StringBuffer();

		if (minute > 0) {
			sb.append(minute + "分");
		}
		if (second > 0) {
			sb.append(second + "秒");
		} else if (milliSecond > 0) {
			sb.append(milliSecond + "毫秒");
		}
		return sb.toString();
	}

	public static void main(String[] args) {

	}
}
